﻿using Anolis.Core.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CALC
{
    public partial class Form1 : Form
    {
        [DllImport("CPPDLL.dll", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false)]
        public static extern uint CalcChecksum(uint checksum, byte[] data, int length);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private uint 常量2;
        private uint 常量3;
        private void test1()
        {
            byte[] v113 = File.ReadAllBytes(textBox5.Text);

            int cl01 = 0x105F091;
            var cl02 = 0xFFB52F77;
            var cl03 = 0xA680CEEF;
            int v114 = cl01 - 0x95F693;// =6FF9FE
            // 6FF9FE + 400 = 6FFDFE
            uint v115 = 0;
            uint v116 = 0;
            if (v114 <= 0)
            {
            }
            else
            {
                var tmp = 0;
                while (true)
                {
                    var v120 = v114;
                    if (v114 > 4096)
                    {
                        v120 = 4096;
                    }
                    uint v118 = 0;
                    uint v119 = 0;
                    var v121 = 0;
                    uint v122 = 0;
                    if (v120 >= 2)
                    {
                        do
                        {
                            uint v123 = movsx(v113[0x400 + tmp + v122++]);
                            v118 += v123;
                            uint v124 = movsx(v113[0x400 + tmp + v122++]);
                            v119 += v124;
                            var t1 = v123 << (v121++ & 0x1F);
                            var t2 = v124 << ((v121++) & 0x1F);
                            v116 ^= t1 ^ t2;
                        } while (v122 < v120 - 1);
                    }
                    if (v122 < v120)
                    {
                        uint v125 = movsx(v113[v122]);
                        v115 += v125;
                        v116 ^= v125 << (v121 & 0x1F);
                    }
                    v115 += v118 + v119;
                    tmp += 4096;
                    v114 -= 4096;
                    if (v114 <= 0)
                    {
                        break;
                    }
                }
            }
            常量2 = v115;
            常量3 = v116;
        }

        private string toHex(uint ii)
        {
            byte[] bData = BitConverter.GetBytes(ii);
            return BitConverter.ToString(bData).Replace("-", "");
        }
        private string toHexR(uint ii)
        {
            byte[] bData = BitConverter.GetBytes(ii);
            Array.Reverse(bData);
            return BitConverter.ToString(bData).Replace("-", "");
        }

        private uint movsx(byte ii)
        {
            if (((ii & 0x80) >> 7) == 1)
            {
                return ii | 0xFFFFFF00;
            }
            return ii;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            test1();
            textBox1.Text = toHex(常量2);
            textBox2.Text = toHex(常量3);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BinaryReader binaryReader = new BinaryReader(new FileStream(textBox5.Text, FileMode.Open));
            binaryReader.BaseStream.Seek(0x3C, SeekOrigin.Begin);
            var OffsetToPEHeader = binaryReader.ReadInt32();
            binaryReader.BaseStream.Seek(OffsetToPEHeader + 0x18 + 0x40, SeekOrigin.Begin);
            var FileChecksum = binaryReader.ReadUInt32();
            binaryReader.Close();
            textBox3.Text = toHexR(FileChecksum);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            BinaryWriter binaryWriter = new BinaryWriter(new FileStream(textBox5.Text, FileMode.Open));
            binaryWriter.BaseStream.Seek(0x9088B8 + 4, SeekOrigin.Begin);
            binaryWriter.Write(常量2);
            binaryWriter.Write(常量3);
            binaryWriter.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var fp = textBox5.Text;

            var hFile = NativeMethods.CreateFile(fp, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
            var hFileMapping = NativeMethods.CreateFileMapping(hFile, IntPtr.Zero, NativeMethods.PAGE_READONLY, 0, 0, "");
            var pBaseAddress = NativeMethods.MapViewOfFile(hFileMapping, NativeMethods.FILE_MAP_READ, 0, 0, IntPtr.Zero);
            IntPtr liSize = IntPtr.Zero;
            var dwSize = NativeMethods.GetFileSize(hFile, liSize);
            uint dwHeaderSum = 0, dwCheckSum = 0;
            NativeMethods.CheckSumMappedFile(pBaseAddress, dwSize, ref dwHeaderSum, ref dwCheckSum);
            NativeMethods.CloseHandle(hFile.DangerousGetHandle());
            textBox4.Text = toHexR(dwCheckSum);
            textBox6.Text = toHexR(dwHeaderSum);
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
            textBox5.Text = file[0];
        }

        private void button5_Click(object sender, EventArgs e)
        {
            /*BinaryReader binaryReader = new BinaryReader(new FileStream(textBox5.Text, FileMode.Open));
            List<byte> bytes = new List<byte>();
            bytes.AddRange(binaryReader.ReadBytes(0x188));
            binaryReader.BaseStream.Position += 4;
            bytes.AddRange(binaryReader.ReadBytes((int)(binaryReader.BaseStream.Length - 0x188 - 4)));
            binaryReader.Close();*/
            var bbb = File.ReadAllBytes(textBox5.Text);// bytes.ToArray();
            List<byte> bs = new List<byte>();
            bs.AddRange(bbb);
            if(bbb.Length % 2 == 1)
            {
                //bs.Add(0x00);
            }
            bbb = bs.ToArray();
            //binaryReader.BaseStream.Position = 0x400;
            var c = (uint)CalcChecksum(0, bbb, bbb.Length);
            textBox7.Text = toHexR(c);
        }
        public static IntPtr BytesToIntptr(byte[] bytes)
        {
            int size = bytes.Length;
            IntPtr buffer = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.Copy(bytes, 0, buffer, size);
                buffer -= size;
                return buffer;
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
    }

}
